package com.ap.core.jbehave.confirmpayment;

import com.ap.core.cxf.jaxb.common.YNType;
import com.ap.core.cxf.jaxb.managepayment.ConfirmPayment1Request;
import com.ap.core.cxf.jaxb.managepayment.ConfirmPayment1Response;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.core.util.Nullable;
import com.core.ap.wsdl.managepaymentservice_1.ManagePayment1PortType;
import org.hamcrest.CoreMatchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.joda.time.LocalDateTime;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Steps class for repeated confirm payment message
 */
public class RepeatedConfirmPaymentSteps extends ConfirmPaymentSteps {

    public RepeatedConfirmPaymentSteps(StoryContext storyContext) {
        super(storyContext);
    }

    @Given("the OSB has submitted a structurally valid but duplicate Confirm Payment message to APCore")
    public void givenTheOSBHasSubmittedStructurallyValidButDuplicateConfirmPaymentMessageToAPCore(){
        confirmPayment1Request.getMsgHdr().setMsgTimestamp(new LocalDateTime());
        confirmPayment1Request.getMsgHdr().setMsgId("New MSG ID");

    }

    @When("APCore receives the duplicate valid CONFIRMPAYMENT request from OSB")
    public void whenAPCoreReceivesTheDuplicateValidCONFIRMPAYMENTRequestFromOSB() throws Exception {
        final ManagePayment1PortType managePayment1PortType = factory.getBean(ManagePayment1PortType.class);
        JaxbUtil.newInstance().javaToXML(ConfirmPayment1Request.class, confirmPayment1Request);
        ConfirmPayment1Response confirmPayment1Response = managePayment1PortType.confirmPayment1(confirmPayment1Request);
        JaxbUtil.newInstance().javaToXML(ConfirmPayment1Response.class, confirmPayment1Response);
        storyContext.put(CONFIRM_PAYMENT_RESPONSE, Nullable.nullable(confirmPayment1Response));
    }

    @Then("the RSPTOPAYMENTCONFIRMATION message with duplicate flag as Y is sent to the OSB")
    public void thenTheRSPTOPAYMENTCONFIRMATIONMessageIsSentToTheOSB() {
        ConfirmPayment1Response confirmPayment1Response = storyContext.get(CONFIRM_PAYMENT_RESPONSE).value();
        doConfirmPaymentResponseAssertions(storyContext.get(CONFIRM_PAYMENT_RESPONSE), confirmPayment1Request, submitRTPRequest);
        assertThat("NotifyDistributor flag must be N", true, CoreMatchers.is(equalTo(confirmPayment1Response.getNotifyRecipientInfo().getNotifyDistributor().equals(YNType.N))));
    }
}
